package com.example.tools.camerautil;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by 70510 on 2016/4/18.
 */
public class BitMap_Camera  {

    //打开系统相册请求码
    public final static int REQUESTCODE_GALLERY = 100;
    //打开系统相机请求码
    public final static int REQUESTCODE_CAMERA = 101;
    //打开前切图片请求码
    public final static int REQUESTCODE_CROP = 102;

    //头像图片保存地址
    public final static String HEADER_URLSTR =
            Environment.getExternalStorageDirectory() + File.separator + "/header2.jpg";

    private Activity activity;

    public BitMap_Camera(Activity activity) {
        this.activity = activity;
    }

    //照相机
    public void gotoCamera() throws Exception{
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //启动时自定义图片保存地址
        Uri outPutUri = getUriFormFile(HEADER_URLSTR);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
        activity.startActivityForResult(intent, REQUESTCODE_CAMERA);
    }
    //相册
    public void gotoGallery() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        activity.startActivityForResult(intent, REQUESTCODE_GALLERY);
    }
    //启动图片剪切
    public void gotoCrop(Uri cameraUri) throws Exception {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(cameraUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);  //设置剪切框的比例
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 150);  //图片输出大小
        intent.putExtra("outputY", 150);  //图片输出大小
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", true);
        activity.startActivityForResult(intent, REQUESTCODE_CROP);

    }

    //获得文件的Uri
    public Uri getUriFormFile(String filePath) {
        return Uri.fromFile(new File(filePath));
    }

    //获取圆角位图的方法
    public Bitmap getRoundCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    /** 保存方法 */
    public static void saveBitmap(Bitmap bm ,String picName) {
        File file = new File(picName);
        if(file.exists()){
            file.delete();
        }
        try {
            file.createNewFile();
            FileOutputStream out = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
